const Service = require('egg').Service;
class UserRoleService extends Service {

  /**
   * 获取用户所有的角色
   * @param payload
   */
  async getUserRoleIds(payload) {
    const { ctx, app } = this;
    const res = await ctx.model.UserRoles.findAll({
      where: { user_id: payload.user_id },
    });
    const roleIds = res.map(e => e.role_id);
    app.redis.sadd(ctx.helper.redisKeys.userRoleIdsBaseUserId(payload.user_id), roleIds)
      .then(() => {
      // 设置3天的过期期限
        app.redis.expire(ctx.helper.redisKeys.userRoleIdsBaseUserId(payload.user_id), 60 * 60 * 24 * 3);
      });
    return roleIds;
  }
}
module.exports = UserRoleService;